﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockFinder.UI.Views;
using StockFinder.DataAccess;
using StockFinder.Model;
using StockFinder.Services.ChartPatterns;

namespace StockFinder.UI.Controllers
{
    public class WeeklyFlatBaseEngineViewController
    {
        private readonly IWeeklyFlatBaseEngineView _View;
        private readonly SymbolDataAccess _SymbolDataAccess;
        private readonly DailyPriceDataAccess _DailyPriceDataAccess;
        private readonly IChartPattern _WeeklyFlatBaseChartPattern;

        public WeeklyFlatBaseEngineViewController(IWeeklyFlatBaseEngineView view)
        {
            _View = view;

            _SymbolDataAccess = new SymbolDataAccess();
            _DailyPriceDataAccess = new DailyPriceDataAccess();
            _WeeklyFlatBaseChartPattern = new WeeklyFlatBaseChartPattern();

            _View.Start += new EventHandler(_View_Start);
        }

        void _View_Start(object sender, EventArgs e)
        {
            //reference to the symbol
            Symbol symbol = null;

            ChartPatternResult chartPatternResult = null;

            DateTime dateFrom =  _View.GetDateFrom();

            //get all symbols
            List<Symbol> symbols = SymbolDataAccess.GetAllSymbols();

            List<Symbol> potentialSymbols = new List<Symbol>();

            //loop the symbols
            for (int i = 0; i < symbols.Count; i++)
            {
                //reference indexed symbol
                symbol = symbols[i];

                //get prices
                _DailyPriceDataAccess.GetAllDailyPricesBySymbolIdFromDate(symbol, dateFrom);

                //generate the weekly prices for this symbol
                symbol.GenerateWeeklyPrices();

                //are there any weekly prices?
                if (symbol.WeeklyPrices != null && symbol.WeeklyPrices.Count > 0)
                {
                    //there are so apply chart pattern
                    chartPatternResult = _WeeklyFlatBaseChartPattern.Apply(symbol);

                    if (chartPatternResult.DoesFit)
                    {
                        potentialSymbols.Add(symbol);
                    }
                }
            }


        }
    }
}
